package com.asdan.seededu.web.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.demo2do.core.web.interceptor.MenuSetting;

@Controller
@MenuSetting("menu-project")
@RequestMapping("/step")
public class PublicController {
	
	@RequestMapping(value="/{filename}/downattachs",method=RequestMethod.GET)
	public void  downattachs(@PathVariable  String filename ,HttpServletResponse res,HttpServletRequest req,Model model) {
		 String realPath =  File.separatorChar+"seededu"+File.separatorChar+"data"+File.separatorChar+"attach"+File.separatorChar+filename;
		   OutputStream os = null ;
	       File file = null ; 
	        try {
	        	file = new File(realPath) ;
	        	os = res.getOutputStream();
	            res.reset();
	            res.setHeader("Content-Disposition", "attachment; filename="+filename);
	            res.setContentType("application/octet-stream; charset=utf-8");
	            os.write(FileUtils.readFileToByteArray(file));
	            os.flush();
	        } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
	            if (os != null) {
	                try {
						os.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						os =null ;
					}
	            }
	        }
	}

}
